<template>
  <div class="app-container">
    <el-row>
      <el-col :span="6">
        <div class="input-container">
          <div class="text-bold ">
            <el-button type="text" class="text-forestgreen margin-right" icon="el-icon-phone" title="只有当后台翻译模块可用时才有用" >翻译</el-button>

            <el-checkbox v-model="setting.trim" >去空格</el-checkbox>
            原始数据:{{handle.left.length}}
          </div>
          <el-input
            type="textarea"
            :rows="18"
            placeholder="原始数据"
            @blur="parseColumns($event)"
            v-model="left">
          </el-input>
        </div>
      </el-col>
      <el-col :span="6" class="margin-left">
        <div class="input-container">
          <div class="text-bold margin-little-bottom">
            <el-button type="text" class="text-forestgreen" icon="el-icon-document-copy" @click="copyText(right,$event)">复制</el-button>
            处理数据:{{handle.right.length}}
          </div>
          <el-input
            type="textarea"
            :rows="18"
            placeholder="处理数据"
            v-model="right">
          </el-input>
        </div>
      </el-col>
      <el-col :span="9" class="margin-left">
        <el-form size="mini" inline>
          <el-form-item>
            <el-checkbox v-model="setting.delRepeat">去除重复数据</el-checkbox>
          </el-form-item>
        </el-form>

        <p class="text-bold">
          <el-button size="small" type="text" class="text-forestgreen" @click="handleColumn">处理</el-button>
          前后缀
        </p>
        <el-form :inline="true">
          <el-form-item >
            <el-input placeholder="前缀" v-model="setting.prefix" size="small" />
          </el-form-item>
          <el-form-item>
            <el-input placeholder="后缀" v-model="setting.suffix" size="small" />
          </el-form-item>
        </el-form>

        <p class="text-bold">
          <el-button size="small" type="text" class="text-forestgreen"  @click="findRow">行处理</el-button>
          查找与替换
        </p>
        <el-form inline>
          <el-form-item class="" style="display: block">
            <el-checkbox v-model="setting.replace.find">仅查找</el-checkbox>
            <el-checkbox v-model="setting.replace.regex">正则表达式</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="查找行" v-model="setting.replace.before" size="small" />
          </el-form-item>
          <el-form-item>
            <el-input placeholder="替换" :disabled="setting.replace.find" v-model="setting.replace.after" size="small" />
          </el-form-item>
        </el-form>

        <el-form >
          <p class="text-bold">
            <el-button size="small" type="text" class="text-forestgreen"  @click="dataCollect">提取</el-button>
            数据提取
          </p>
          <el-form-item >
            <el-input size="small" placeholder="提取正则" v-model="setting.dataCollect.select.regex" class="input-with-select">
              <el-select style="width: 100px" slot="prepend" v-model="setting.dataCollect.select.label"  @change="choseSample" placeholder="请选择">
                <el-option v-for="sample in samples" :key="sample.label" :label="sample.label" :value="sample.label" />
              </el-select>
              <el-select style="width: 60px"  class="small" slot="append" v-model="setting.dataCollect.select.index" placeholder="正则分组">
                <el-option v-for="index in [0,1,2,3,4,5,6]" :key="'group'+index" :label="index" :value="index" />
              </el-select>
            </el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import clip from "@/utils/clipboard";

function inArray(val,array){
  if (!array || array.length === 0){
    return -1;
  }
  for (let i = 0; i < array.length; i++) {
    if (array[i] === val){
      return i;
    }
  }
  return -1;
}
export default {
  name: "column",
  data(){
    return {
      input3: '',
      select: '',
      left: null,
      right: null,
      handle:{
        left: [],
        right: []
      },
      setting:{
        trim: true,
        prefix: '',
        suffix: '',
        delRepeat: true,
        replace:{
          before: null,
          after: null,
          regex: true,
          find: true
        },
        dataCollect:{
          select: {regex:null,label: null,index: 0},
          samples:{
            'Java属性':{pattern:/(private|protected)\s+\w+\s+(\w+);/,index:2},
            'Java 属性值':{pattern:/(private|protected|public).*?=\s*(.+?);/,index:2},
            'Java 公共属性':{pattern:/public.*?\s(\w+)\s=/,index:1},
            '文档注释':{pattern:/\* (.*?)\n/,index:1},
            '末尾注释':{pattern:/\/\/(.*)/,index:1},
            '末尾注释前面的':{pattern:/(.*)\/\//,index:1},
            '属性列':{pattern: /property="(\w+)"/,index:1},
            '数据库列':{pattern:/column="(\w+)"/,index:1},
            'temp':{pattern:/\w\.(\w+),/,index:1},
            '表列':{pattern:/"(\w+)"\s.*/,index:1},
            '表列类型':{pattern:/"(\w+)"\s(\w+).*/,index:2},
            '表列注释':{pattern:/\'(.+)\';/,index:1},
            'hibernatesql':{pattern:/"(.+)"/,index:1},
            'aliassql':{pattern:/(\w+),/,index:1},
            '注释提取':{pattern:/value\s+=\s+"(.+)"/,index:1},
            '类型提取':{pattern:/(private|protected)\s+(\w+)/,index:2}
          }
        }
      }
    }
  },
  methods: {
    copyText(text, event){
      clip(text, event)
    },
    choseSample(val){
      let dataCollect = this.setting.dataCollect;
      if (dataCollect.samples[val]){
        dataCollect.select.regex = dataCollect.samples[val].pattern.source;
        dataCollect.select.index = dataCollect.samples[val].index;
      }
    },
    parseColumns(event){
      let value = event.target.value.trim().split('\n');

      let handleValue = [];
      for (let i = 0; i < value.length; i++) {
        let item = value[i];
        if (this.setting.trim){
          item = item.trim();
        }
        if (item){
          handleValue.push(item);
        }
      }

      this.handle['left']  = handleValue;
    },
    handleRepeat(){
      let rightDatas = this.handle.left;

      if (this.setting.delRepeat){
        // 去除重复数据
        let afterResult = [];
        for (let i = 0; i < this.handle.left.length; i++) {
          let item = this.handle.left[i];
          if (inArray(item,afterResult) != -1){
            continue;
          }
          afterResult.push(item);
        }
        rightDatas = afterResult;
      }
      return rightDatas;
    },
    handleColumn(){
      let rightDatas = this.handleRepeat();

      // 前后缀操作
      if (this.setting.prefix || this.setting.suffix){
        for (let i = 0; i < rightDatas.length; i++) {
          rightDatas[i] = this.setting.prefix + rightDatas[i] + this.setting.suffix;
        }
      }

      this.handle.right = rightDatas;
      this.right = rightDatas.join('\n');
    },
    findRow(){
      let rightDatas = this.handleRepeat();

      // 数据行处理
      if (this.setting.replace.before){
        let handleResult = [];
        if (this.setting.replace.find){
          // 仅查询操作 ,将查询出的结果显示即可
          for (let i = 0; i < rightDatas.length; i++) {
            let item = rightDatas[i];
            if (!this.setting.replace.regex && item.contains(this.setting.replace.before)){
              handleResult.push(item);
            }else if (this.setting.replace.regex ){
              let regex = new RegExp(this.setting.replace.before);
              if (regex.test(item)){
                handleResult.push(item);
              }

            }
          }
        }else{
          // 需要替换的情况
          for (let i = 0; i < rightDatas.length; i++) {
            let item = rightDatas[i];
            if (!this.setting.replace.regex && item.contains(this.setting.replace.before)){
              handleResult.push(item.replaceAll(this.setting.replace.before,this.setting.replace.after));
            }else if (this.setting.replace.regex && new RegExp(this.setting.replace.before).test(item)){
              alert('正则替换暂不支持')
            }
          }
        }
        rightDatas = handleResult;
      }

      this.handle.right = rightDatas;
      this.right = rightDatas.join('\n');
    },

    // 数据提取功能
    dataCollect(){
      let rightDatas = this.handleRepeat();

      let finalDatas = [];
      for (let i = 0; i < rightDatas.length; i++) {
        let item = rightDatas[i];
        let regExpExecArray = new RegExp(this.setting.dataCollect.select.regex).exec(item);
        if (regExpExecArray && regExpExecArray.length > this.setting.dataCollect.select.index){
          let group = regExpExecArray[this.setting.dataCollect.select.index];
          finalDatas.push(group);
        }
      }

      this.handle.right = finalDatas;
      this.right = finalDatas.join('\n');
    }
  },
  computed:{
    samples(){
      let samples = [];
      for (let label in this.setting.dataCollect.samples){
        samples.push({label:label,value:this.setting.dataCollect.samples[label].pattern})
      }
      return samples;
    }
  }
}
</script>

<style scoped>
/deep/ .input-container textarea{
  white-space: nowrap;
  overflow: scroll;
}
</style>
